﻿using Mapster;
using ZR.Model;
using ZR.Model.Souvenir;
using ZR.Model.Souvenir.Dto;
using ZR.Service.Souvenir.IService;
using ZR.ServiceCore.Model;

namespace ZR.Service.Souvenir
{
    [AppService(ServiceType = typeof(ICustomerFeedbackService), ServiceLifetime = LifeTime.Transient)]
    public class CustomerFeedbackService : BaseService<CustomerFeedback>, ICustomerFeedbackService
    {
        public PagedInfo<CustomerFeedbackDto> SelectCustomerFeedbackWithPage(CustomerFeedbackQueryDto parm)
        {
            var exp = Expressionable.Create<CustomerFeedback>();
            exp.AndIF(parm.CustomerId != 0, cf => cf.CustomerId == parm.CustomerId);
            var res = Queryable()
                .LeftJoin<SysUser>((cf, su) => cf.CustomerId == su.UserId)
                .Where(exp.ToExpression())
                .Select((cf, su) => new CustomerFeedbackDto()
                {
                    Id = cf.Id,
                    Message = cf.Message,
                    CustomerId = cf.CustomerId,
                    CreateTime = cf.CreateTime,
                    CustomerName = su.NickName
                })
                .ToPageList(parm.PageNum, parm.PageSize);
            return new PagedInfo<CustomerFeedbackDto>()
            {
                PageIndex = parm.PageNum,
                PageSize = parm.PageSize,
                Result = res,
                TotalNum = res.Count
            };
        }

        public bool InsertCustomerFeedback(CustomerFeedbackDto parm)
        {
            return Insertable(parm.Adapt<CustomerFeedback>()).ExecuteReturnSnowflakeId() > 0;
        }

        public bool UpdateCustomerFeedback(CustomerFeedbackDto parm)
        {
            return Updateable(parm.Adapt<CustomerFeedback>()).ExecuteCommand() > 0;
        }

        public bool DeleteCustomerFeedback(long id)
        {
            return DeleteById(id);
        }
    }
}